from django.conf import settings
from django.conf.urls.defaults import *
from django.contrib.syndication.views import feed as feed_view
from django.views.generic.date_based import archive_year, archive_month, archive_day
from django.views.generic.list_detail import object_list as list_view
from django.views.generic.list_detail import object_detail as detail_view
from django.views.generic.simple import redirect_to
from pyplog.artists.models import Artist as Author
from pyplog.blog.feeds import BlogPosts, BlogPostsByAuthor, BlogPostsByCategory
from pyplog.blog.models import Category, Post
from pyplog.blog.views import key_filter

blog_posts = Post.public_objects.all()

archive_parameters = {
	"queryset" : blog_posts,
	"date_field" : "published",
	"extra_context" : { "page_title" : "Blog archives" },
	"template_name" : "blog/post_list.html",
	"template_object_name" : "post",
}
archive_year_parameters = archive_parameters.copy()
archive_year_parameters.update({ "make_object_list" : True })
archive_month_parameters = archive_parameters.copy()
archive_month_parameters.update({ "month_format" : "%m" })
archive_day_parameters = archive_month_parameters

feeds = {
	"all" : BlogPosts,
	"author" : BlogPostsByAuthor,
	"category" : BlogPostsByCategory,
}

urlpatterns = patterns("",
	url(r"^(?P<page>\d+)?/?$", key_filter, name = "blog-index"),
	url(r"^archive/?$", redirect_to, { "url" : "/blog/" }, name = "archive-index"),
	url(
		r"^archive/(?P<year>\d{4})/$",
		archive_year,
		archive_year_parameters,
		name = "archive-year",
	),
	url(
		r"^archive/(?P<year>\d{4})/(?P<month>\d{2})/$",
		archive_month,
		archive_month_parameters,
		name = "archive-month",
	),
	url(
		r"^archive/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/$",
		archive_day,
		archive_day_parameters,
		name = "archive-day",
	),
	url(r"^feed/(?P<url>.*)/$", feed_view, { "feed_dict" : feeds }, name = "post-feed"),
	url(
		r"^post/(?P<slug>.*)$",
		detail_view,
		{
			"queryset" : blog_posts,
			"template_name" : "blog/post_detail.html",
			"template_object_name" : "post",
		},
		name = "post-detail",
	),
	url(r"^author/(?P<slug>[^/]+)/(?P<page>\d*)/?$", key_filter, { "key_class" : Author }, name = "author-posts"),
	url(r"^category/(?P<slug>[^/]+)/(?P<page>\d*)/?$", key_filter, { "key_class" : Category }, name = "category-posts"),
)
